package cn.yunhe.exception;

import cn.yunhe.model.ResultModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private Logger logger = LogManager.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public ResultModel<?> ExceptionHandle(Exception e){
        logger.info("系统繁忙");
        ResultModel<Void> rm = new ResultModel<Void>();
        rm.setCode(ErrCode.GL_SYS_BUSY.code);
        rm.setMsg(ErrCode.GL_SYS_BUSY.msg);
        logger.error(e.getMessage());
        return rm;
    }

    @ExceptionHandler(NullPointerException.class)
    public ResultModel<?> ExceptionHandle1(NullPointerException e){
        logger.info("空指针异常");
        ResultModel<Void> rm = new ResultModel<Void>();
        rm.setCode(ErrCode.GL_SYS_NULL.code);
        rm.setMsg(ErrCode.GL_SYS_NULL.msg);
        logger.error(e.getMessage());
        return rm;
    }

    @ExceptionHandler(NumberFormatException.class)
    public ResultModel<?> ExceptionHandle2(NumberFormatException e){
        logger.info("数值转换异常");
        ResultModel<Void> rm = new ResultModel<Void>();
        rm.setCode(ErrCode.GL_SYS_FORMAT_ERR.code);
        rm.setMsg(ErrCode.GL_SYS_FORMAT_ERR.msg);
        logger.error(e.getMessage());
        return rm;
    }

    @ExceptionHandler(PwdException.class)
    public ResultModel<?> ExceptionHandle2(PwdException e){
        logger.info("用户密码错误");
        ResultModel<Void> rm = new ResultModel<Void>();
        rm.setCode(ErrCode.USER_PWD_ERR.code);
        rm.setMsg(ErrCode.USER_PWD_ERR.msg);
        logger.error(e.getMessage());
        return rm;
    }
}
